package com.sjgs.gis.service;

import com.sjgs.gis.domain.War;
import com.sjgs.gis.repository.WarRepository;
import com.sjgs.gis.service.dto.WarDTO;
import com.sjgs.gis.service.mapper.WarMapper;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;

import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.Optional;

/**
 * Service Implementation for managing {@link War}.
 */
@Service
@Transactional
public class WarService {

    private final Logger log = LoggerFactory.getLogger(WarService.class);

    private final WarRepository warRepository;

    private final WarMapper warMapper;

    public WarService(WarRepository warRepository, WarMapper warMapper) {
        this.warRepository = warRepository;
        this.warMapper = warMapper;
    }

    /**
     * Save a war.
     *
     * @param warDTO the entity to save.
     * @return the persisted entity.
     */
    public WarDTO save(WarDTO warDTO) {
        log.debug("Request to save War : {}", warDTO);
        War war = warMapper.toEntity(warDTO);
        war = warRepository.save(war);
        return warMapper.toDto(war);
    }

    /**
     * Get all the wars.
     *
     * @param pageable the pagination information.
     * @return the list of entities.
     */
    @Transactional(readOnly = true)
    public Page<WarDTO> findAll(Pageable pageable) {
        log.debug("Request to get all Wars");
        return warRepository.findAll(pageable)
            .map(warMapper::toDto);
    }


    /**
     * Get one war by id.
     *
     * @param id the id of the entity.
     * @return the entity.
     */
    @Transactional(readOnly = true)
    public Optional<WarDTO> findOne(Long id) {
        log.debug("Request to get War : {}", id);
        return warRepository.findById(id)
            .map(warMapper::toDto);
    }

    /**
     * Delete the war by id.
     *
     * @param id the id of the entity.
     */
    public void delete(Long id) {
        log.debug("Request to delete War : {}", id);
        warRepository.deleteById(id);
    }
}
